<template>
  <div class="app-container drag-container">
    <el-row :gutter="0">
      <el-col :xs="24" :sm="6" :md="5" :lg="4" class="drag-area" id="drag-area">
        <div style="text-align: center; color: #0fa8e7">数据选择</div>

        <el-input
          style="width: 80%"
          prefix-icon="el-icon-search"
          placeholder="输入关键字"
          v-model="filterText"
        >
        </el-input>

        <el-tree
          class="filter-tree"
          :data="data"
          :props="defaultProps"
          @node-click="handleNodeClick"
          :filter-node-method="filterNode"
          ref="tree"
        ></el-tree>

        <div class="drag-tip" id="drag-tip"></div>
      </el-col>

      <el-col :xs="24" :sm="18" :md="18.7" :lg="20" id="drag-content">
        <el-form
          :inline="true"
          :model="queryParams"
          ref="queryForm"
          v-show="showSearch"
          label-width="100px"
          class="demo-form-inline"
        >
          <!-- <el-form-item label="学年名称" prop="schoolYearId">
            <el-select
              v-model="queryParams.schoolYearId"
              placeholder="请选择学年"
              clearable
              @change="searchTermByYear()"
              size="small"
              style="width: 240px"
            >

              <el-option
                v-for="item in yearOptions"
                :key="item.index"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="学期名称" prop="termId">
            <el-select
              v-model="queryParams.termId"
              placeholder="请选择学期"
              clearable
              size="small"
              style="width: 240px"
              @change="searchCollegeByTerm()"
            >
              <el-option
                v-for="item in termNameOptions"
                :key="item.index"
                :label="item.termName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="学院名称" prop="collegeId">
            <el-select
              v-model="queryParams.collegeId"
              placeholder="请选择学院"
              clearable
              size="small"
              style="width: 240px"

            >
              <el-option
                v-for="item in collegeOptions"
                :key="item.index"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item> -->

          <el-form-item label="教师名称" prop="userId">
            <el-select
              v-model="queryParams.userId"
              placeholder="请选择教师"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="item in teacherOptions"
                :key="item.index"
                :label="item.teacherName"
                :value="item.userId"
              />
            </el-select>
            <!-- <el-input v-model="queryParams.teacher" placeholder="教师名称"></el-input> -->
          </el-form-item>

          <el-form-item label="课程名称" prop="courseId">
            <el-select
              v-model="queryParams.courseId"
              placeholder="请选择课程"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="item in courseOptions"
                :key="item.index"
                :label="item.courseName"
                :value="item.id"
              />
            </el-select>
            <!-- <el-input v-model="queryParams.course" placeholder="课程名称"></el-input> -->
          </el-form-item>

          <el-form-item label="课程编码" prop="courseCode">
            <el-input
              v-model="queryParams.courseCode"
              placeholder="请输入课程编码"
              clearable
              style="width: 240px"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['org:course:add']"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['org:course:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['org:course:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              :loading="exportLoading"
              @click="handleExport"
              v-hasPermi="['org:course:export']"
              >导出</el-button
            >
          </el-col>
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>

        <el-table :data="courseList" style="width: 100%">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column prop="courseCode" label="课程编码" width="180">
          </el-table-column>
          <el-table-column prop="teacherName" label="教师名称" width="180">
          </el-table-column>

          <el-table-column prop="courseName" label="课程名称" width="180">
          </el-table-column>

          <!-- <el-table-column prop="schoolYearName" label="开课学年" width="180">
          </el-table-column>

          <el-table-column prop="termName" label="开课学期" width="180">
          </el-table-column>

          <el-table-column prop="collegeName" label="开课学院" width="180">
          </el-table-column> -->

          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['org:course:edit']"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['org:course:remove']"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
      <!-- 新增教师对应的课程 -->
      <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
        <el-form
          label-position="left"
          label-width="80px"
          :model="form"
          ref="form"
          :rules="rules"
        >
          <!-- <el-form-item label="学年名称" prop="schoolYear">
              <el-select
              v-model="form.schoolYearId"
              placeholder="请选择学年"
              clearable
              size="small"
              style=" width: 100%;"
              @change="addTermByYear()"
              >
              <el-option
              v-for="item in yearOptions"
              :key="item.index"
              :label="item.name"
              :value="item.id"
              />
              </el-select>
            </el-form-item>

            <el-form-item label="学期名称" prop="termId">
              <el-select
              v-model="form.termId"
              placeholder="请选择学期"
              clearable
              size="small"
              style="width: 100%;"
              @change="addCollegeByTerm()"
              >
              <el-option
              v-for="item in termNameOptions"
              :key="item.index"
              :label="item.termName"
              :value="item.id"
              />
              </el-select>
            </el-form-item>

            <el-form-item label="学院名称" prop="collegeId">
              <el-select
              v-model="form.collegeId"
              placeholder="请选择学院"
              clearable
              size="small"
              style="width: 100%;"
              @change="addMyTeacherCourse()"
              >
              <el-option
              v-for="item in collegeOptions"
              :key="item.index"
              :label="item.name"
              :value="item.id"
              />
              </el-select>
            </el-form-item> -->

          <!-- <el-form-item label="教师">
             <el-cascader  v-model="form.teacher"  :options="teacherOptions" clearable></el-cascader>
          </el-form-item> -->

          <el-form-item label="教师名称">
            <!-- <el-select
              v-model="form.userId"
              placeholder="请选择教师"
              clearable
              size="small"
              style="width: 100%"
              @change="handleChange('form', 'userId')"
            >
              <el-option
                v-for="item in teacherOptions"
                :key="item.index"
                :label="item.teacherName"
                :value="item.userId"
              />
            </el-select> -->
            <div style="display: flex">
              <el-input
                v-model="teacherNames"
                :disabled="true"
                style="width: 60%; margin-right: 8%"
              ></el-input>
              <el-button @click="showTeacher = !showTeacher" type="primary"
                >选择教师</el-button
              >
            </div>
          </el-form-item>

          <el-form-item label="课程名称" prop="courseId">
            <!-- <el-select
              v-model="form.courseId"
              placeholder="请选择课程"
              clearable
              size="small"
              style="width: 100%"
              @change="handleChange('form', 'courseId')"
            >
              <el-option
                v-for="item in courseOptions"
                :key="item.index"
                :label="item.courseName"
                :value="item.id"
              />
            </el-select> -->
            <div style="display: flex">
              <el-input
                v-model="courseBaseName"
                :disabled="true"
                style="width: 60%; margin-right: 8%"
              ></el-input>
              <el-button @click="showBaseClass = !showBaseClass" type="primary"
                >选择课程</el-button
              >
            </div>
          </el-form-item>
          <!-- <el-form-item label="课程">
             <el-cascader v-model="form.course"  :options="courseOptions" clearable></el-cascader>
          </el-form-item> -->
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="cancel()">取 消</el-button>
          <el-button type="primary" @click="submitForm()">确 定</el-button>
        </span>
      </el-dialog>

      <el-dialog :visible.sync="showTeacher" width="80%">
        <Teacher :showAddBtn="false" @selectTeacher="selectTeacher" />
      </el-dialog>

      <el-dialog :visible.sync="showBaseClass" width="80%">
        <Course :showSelect="false" @selectCourse="selectCourse" />
      </el-dialog>
    </el-row>
  </div>
</template>

<script>
import {
  listCourse as listTeacherCourse,
  getCourse,
  delCourse,
  addCourse,
  updateCourse,
  exportCourse,
} from "@/api/system/course";

import { listTeacher } from "@/api/system/teacher";
import { listCourse } from "@/api/org/course";

import { course } from "@/views/mixins/course/index";

import Teacher from "../../system/teacher/index";
import Course from "../course/index";

export default {
  name: "teacherCourse",
  mixins: [course],
  components: {
    Teacher,
    Course,
  },
  data() {
    return {
      // 显示选择教师
      showTeacher: false,
      // 显示学生选择
      showBaseClass: false,
      teacherNames: "", // 选择的教师名称
      courseBaseName: "", // 课程名称
      data: [
        {
          label: "信息工程学院",
          children: [
            {
              label: "张三",
            },
            {
              label: "李四",
            },
            {
              label: "王五",
            },
            {
              label: "赵六",
            },
          ],
        },
        {
          label: "数学学院",
          children: [
            {
              label: "张三",
            },
            {
              label: "李四",
            },
            {
              label: "王五",
            },
            {
              label: "赵六",
            },
          ],
        },
        {
          label: "物理学院",
          children: [
            {
              label: "张三",
            },
            {
              label: "李四",
            },
            {
              label: "王五",
            },
            {
              label: "赵六",
            },
          ],
        },
      ],

      defaultProps: {
        children: "children",
        label: "label",
      },
      formInline: {
        teacher: "",
        course: "",
      },
      tableData: [
        {
          teacher: "张三",
          course: "数据结构",
          count: "100人",
          attribute: "必修",
        },
        {
          teacher: "李四",
          course: "数据逻辑",
          count: "100人",
          attribute: "选修",
        },
        {
          teacher: "王五",
          course: "算法",
          count: "100人",
          attribute: "选修",
        },
        {
          teacher: "王五",
          course: "计算机导论",
          count: "100人",
          attribute: "必修",
        },
      ],
      // 总条目数
      total: 10,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 教师课程管理表格数据
      courseList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schoolYearId: null,
        collegeId: null,
        termId: null,
        courseId: null,
        userId: null,
        courseName: null,
        courseCode: null,
        startingUnit: null,
        courseNature: null,
        courseAttribute: null,
        courseCredit: null,
      },
      // 添加教师对应课程的弹框
      open: false,
      form: {
        schoolYearId: null,
        collegeId: null,
        termId: null,
        userId: null,
        courseId: null,
        id: null,
      },
      // 表单校验
      rules: {
        userId: [{ required: true, message: "请选择教师", trigger: "blur" }],
        courseId: [{ required: true, message: "请选择课程", trigger: "blur" }],
      },

      teacherOptions: [
        {
          value: "信息工程学院",
          label: "信息工程学院",
          children: [
            {
              value: "张三",
              label: "张三",
            },
            {
              value: "李四",
              label: "李四",
            },
            {
              value: "王五",
              label: "王五",
            },
            {
              value: "赵六",
              label: "赵六",
            },
          ],
        },
        {
          value: "数学学院",
          label: "数学学院",
          children: [
            {
              value: "张三",
              label: "张三",
            },
            {
              value: "李四",
              label: "李四",
            },
            {
              value: "王五",
              label: "王五",
            },
            {
              value: "赵六",
              label: "赵六",
            },
          ],
        },
        {
          value: "物理学院",
          label: "物理学院",
          children: [
            {
              value: "张三",
              label: "张三",
            },
            {
              value: "李四",
              label: "李四",
            },
            {
              value: "王五",
              label: "王五",
            },
            {
              value: "赵六",
              label: "赵六",
            },
          ],
        },
      ],

      courseOptions: [
        {
          value: "数据结构",
          label: "数据结构",
        },
        {
          value: "算法",
          label: "算法",
        },
        {
          value: "计算机导论",
          label: "计算机导论",
        },
      ],
      // 开课日期
      dateOptions: [
        {
          value: "2017学年",
          label: "2017学年",
          children: [
            {
              value: "上学期",
              label: "上学期",
            },
            {
              value: "下学期",
              label: "下学期",
            },
          ],
        },
        {
          value: "2018学年",
          label: "2018学年",
          children: [
            {
              value: "上学期",
              label: "上学期",
            },
            {
              value: "下学期",
              label: "下学期",
            },
          ],
        },
        {
          value: "2019学年",
          label: "2019学年",
          children: [
            {
              value: "上学期",
              label: "上学期",
            },
            {
              value: "下学期",
              label: "下学期",
            },
          ],
        },
      ],
      title: "",
      //课程信息
      courseOptions: [
        {
          id: "",
          name: "",
        },
      ],
      teacherOptions: [
        {
          userId: "",
          teacherName: "",
        },
      ],
      filterText: "",
      defaultProps: {
        children: "children",
        label: "label",
      },
    };
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getList();
    this.getTeacherOptions();
  },
  mounted() {
    console.log("resetForm: ", this.resetForm);
  },
  methods: {
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 选择教师
    selectTeacher(row) {
      this.form.userId = row.userId;
      this.teacherNames = row.teacherName;
      this.showTeacher = false;
    },
    // 选择课程
    selectCourse(row) {
      this.form.courseId = row.id;
      this.courseBaseName = row.courseName;
      this.showBaseClass = false;
    },

    resetCourseOptions() {
      this.courseOptions = [
        {
          id: "",
          name: "",
        },
      ];
    },
    resetTeacherOptions() {
      this.teacherOptions = [
        {
          userId: "",
          teacherName: "",
        },
      ];
    },
    /** 查询教师课程管理列表 */
    getList() {
      this.loading = true;
      listTeacherCourse(this.queryParams).then((response) => {
        this.courseList = response.rows;
        console.log("courseList: ", this.courseList);
        this.total = response.total;
        this.loading = false;
      });
    },
    getTeacherOptions() {
      (this.form.userId = null), this.resetTeacherOptions();
      listTeacher().then((res) => {
        this.teacherOptions = res.rows;
        console.log("teacherOptions:", this.teacherOptions);
      });
    },
    // 搜索栏中根据选择的学年学期学院查询下面的课程的教师信息
    // searchTeacherCourse(){
    //   console.log("选择了学院");
    //   // this.queryParams.courseId=null,
    //   // this.resetCourseOptions();
    //   // this.queryParams.userId=null,
    //   // this.resetTeacherOptions();

    //   // listCourse(this.queryParams).then((res) => {
    //   //      this.courseOptions = res.rows;
    //   //      console.log("courseOptions:",this.courseOptions);
    //   // });

    //   // listTeacher({
    //   //   pageNum: 1,
    //   //   pageSize: 10,
    //   // }).then((res) => {
    //   //   this.teacherOptions = res.rows;
    //   //   console.log("teacherOptions:",this.teacherOptions);
    //   // });

    // },
    // 搜索栏中根据选择的学年学期学院查询下面的课程的教师信息
    // addMyTeacherCourse(){
    //   this.form.courseId=null,
    //   this.resetCourseOptions();
    //   console.log("查询中的form: ",this.form);

    //   listCourse(this.form).then((res) => {
    //     this.courseOptions = res.rows;
    //     console.log("courseOptions:",this.courseOptions);
    //   });

    //   // this.form.userId=null,
    //   // this.resetTeacherOptions();
    //   // listTeacher({
    //   //   pageNum: 1,
    //   //   pageSize: 10,
    //   // }).then((res) => {
    //   //   this.teacherOptions = res.rows;
    //   //   console.log("teacherOptions:",this.teacherOptions);
    //   // });
    // },

    handleNodeClick(data) {
      console.log(data);
    },
    // 表单重置
    reset() {
      this.form = {
        schoolYearId: null,
        collegeId: null,
        termId: null,
        userId: null,
        courseId: null,
        id: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleAdd() {
      this.reset();
      this.title = "新增课程教师信息";
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getCourse(id).then((response) => {
        this.form = response.data;
        console.log("获取的form信息： ", this.form);
        this.open = true;
        this.title = "修改教师课程管理";

        this.form.courseId = response.data.orgCourse.id;
        this.form.userId = response.data.sysTeacher.userId;

        // this.termNameOptions[0].id = response.data.termId;
        // this.termNameOptions[0].termName = response.data.termName;
        // this.collegeOptions[0].id = response.data.collegeId;
        // this.collegeOptions[0].name = response.data.collegeName;
        // this.courseOptions[0].id=response.data.orgCourse.id;
        // this.courseOptions[0].courseName=response.data.orgCourse.courseName;
        // this.teacherOptions[0].userId=response.data.sysTeacher.userId;
        // this.teacherOptions[0].teacherName=response.data.sysTeacher.teacherName;
      });
    },
    editDialog(id) {
      this.title = "修改课程教师信息";
      this.open = true;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateCourse(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourse(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除教师课程管理编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delCourse(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有教师课程管理数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          // return exportCourse({pageNum: 1,pageSize: 10});
          return exportCourse(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>

<style>
::v-deep .dialog-footer:after {
  content: "";
  display: table;
  clear: both;
}
</style>
